package com.wzcl.app.controller.student;

import com.wzcl.app.dao.model.CourseComment;
import com.wzcl.app.dao.repository.AppUserRepository;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.CourseCommentDto;
import com.wzcl.app.model.vo.CourseCommentVo;
import com.wzcl.app.service.CourseCommentService;
import com.wzcl.app.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping("/userComment")
@Api(value = "课程评论管理controller", tags = {"课程评论管理"})
public class UserCommentController {

    @Autowired
    private CourseCommentService service;
    @Autowired
    private AppUserRepository appUserRepository;

    @ApiOperation("新增评论")
    @PostMapping("/add")
    public RVO<CourseCommentVo> addCourseComment(@RequestBody @Valid CourseCommentDto courseCommentDto){
        //获取用户id并写入
        //数据格式：UserDetailsImpl(id=2, phone=15872591013, password=null, nickName=田老师, realName=田老师, userState=0, state=0, type=null, grantedAuthorities=[])
        String appUser = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        //截取userId的值
        String userId = StringUtil.subString(appUser, "id=", ", phone");
        //设置用户id、头像、电话
        courseCommentDto.setUserId(Integer.valueOf(userId));
        courseCommentDto.setUserHeadIcon(appUserRepository.findHeadIconByUserId(Integer.valueOf(userId)));
        courseCommentDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        CourseComment comment = service.addCourseComment(courseCommentDto);
        return RVO.success(CourseCommentVo.create(comment));
    }

    @ApiOperation("删除评论")
    @PostMapping("/del")
    public RVO<CourseCommentVo> delCourseComment(@RequestBody CourseCommentDto courseCommentDto){
        courseCommentDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        CourseComment comment = service.delCourseComment(courseCommentDto);
        return RVO.success(CourseCommentVo.create(comment));
    }

    @ApiOperation("查看评论")
    @PostMapping("/get")
    public RVO<CourseCommentVo> getCourseComment(@RequestBody CourseCommentDto courseCommentDto){
        CourseComment comment = service.getCourseCommentTreeById(courseCommentDto.getId());
        return RVO.success(CourseCommentVo.create(comment));
    }

    @ApiOperation("查看评论Page")
    @PostMapping("/getPage")
    public RVO getCourseCommentPage(@RequestBody CourseCommentDto courseCommentDto){
        MyPage<CourseComment> page = service.getCourseCommentPage(courseCommentDto);
        if(page != null && page.getContent().size()>0){
            List<CourseCommentVo> collect = page.getContent().stream().map(courseComment ->{service.putCourseCommentTree(courseComment);
            return CourseCommentVo.create(courseComment);}).collect(Collectors.toList());
            MyPage<CourseCommentVo> myPage = new MyPage<>();
            myPage.setContent(collect);
            myPage.setTotalElements(collect.size());
            return RVO.success(myPage);
        }
        return RVO.success(null);
    }
}